<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: huajie <banhuajie@163.com>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Admin\Model\AuthGroupModel;

/**
 * 后台内容控制器
 * @author huajie <banhuajie@163.com>
 */
class ActController extends AdminController {

    public function index()
    {
        $m_lecture = M('lecture');
        $list = $m_lecture->order('id desc')->select();
        $this->assign('_list', $list);
        $this->display();
    }

    /**
     * 添加
     */
    public function add()
    {
        if (IS_GET) {
            $this->display();
        } elseif (IS_POST) {
            $m_lecture = M('lecture');
            $m_lecture->create();

            if(!empty($_FILES['image']['size'])) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      UPLOAD_PATH . 'image/'; // 设置附件上传根目录
                // dump($upload->rootPath);die;
                if (!file_exists($upload->rootPath)) {
                    mkdir($upload->rootPath, 0777, true);
                }
                $upload->saveName = time().'_'.mt_rand();
                // 上传单个文件
                $info   =   $upload->uploadOne($_FILES['image']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $file_path = '/Uploads/image/' . $info['savepath'].$info['savename'];
                    // dump($info);die;
                    $has_file = true;
                }
                $data = $m_lecture->data();
                $data['image'] = $file_path;
                $m_lecture->data($data);

            }
            // dump($m_lecture);die;

            $m_lecture->add();
            $this->redirect('index');
        }
    }

    public function edit($id =0)
    {
        if (IS_GET) {
            $m_lecture = M('lecture');
            $info = $m_lecture->getById($id);
            $this->assign('info', $info);
            $this->display();
        } elseif (IS_POST) {
            $m_lecture = M('lecture');
            $m_lecture->create();

            if(!empty($_FILES['image']['size'])) {
                $upload = new \Think\Upload();// 实例化上传类
                $upload->maxSize   =     3145728 ;// 设置附件上传大小
                $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
                $upload->rootPath  =      UPLOAD_PATH . 'image/'; // 设置附件上传根目录
                // dump($upload->rootPath);die;
                if (!file_exists($upload->rootPath)) {
                    mkdir($upload->rootPath, 0777, true);
                }
                $upload->saveName = time().'_'.mt_rand();
                // 上传单个文件
                $info   =   $upload->uploadOne($_FILES['image']);
                if(!$info) {// 上传错误提示错误信息
                    $this->error($upload->getError());
                }else{// 上传成功 获取上传文件信息
                    $file_path = '/Uploads/image/' . $info['savepath'].$info['savename'];
                    // dump($info);die;
                    $has_file = true;
                }
                $data = $m_lecture->data();
                $data['image'] = $file_path;
                $m_lecture->data($data);

            }

            $m_lecture->save();
            $this->redirect('index');
        }
    }

    public function del($id = 0)
    {
        if (IS_AJAX) {
            $m_lecture = M('lecture');
            $m_lecture->where('id = '.$id)->delete();
            $info = array(
                'status' => 1,
                'info' => '删除成功',
                'url' => U('index')
            );
            $this->ajaxReturn($info);

        }
    }

}